package com.axis2.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author yemulin
 * @create 2012-8-20 下午3:01:43
 */
public class PropertiesUtil {

	private static Logger loggger = LoggerFactory
			.getLogger(PropertiesUtil.class);

	private static Properties props;

	/*
	 * public static void load(String fileName) { try { InputStream in =
	 * Thread.currentThread
	 * ().getContextClassLoader().getResourceAsStream(fileName); props = new
	 * Properties(); props.load(in); } catch (Exception e) {
	 * e.printStackTrace(); } }
	 */

	public static Properties load(String fileName) {
		Properties props = null;
		try {
			InputStream in = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream(fileName);
			props = new Properties();
			props.load(in);
		} catch (Exception e) {
			loggger.error("加载配置文件{}出错:{}", new Object[] { fileName, e });
		}
		return props;
	}

	public static Properties load(URL url) {
		Properties props = null;
		try {
			props = new Properties();
			props.load(url.openStream());
		} catch (Exception e) {
			loggger.error("加载配置文件{}出错:{}", new Object[] { e });
		}
		return props;
	}

	/**
	 * 获取某个属性
	 */
	public static String getProperty(String key) {
		return props.getProperty(key);
	}

	/**
	 * 获取某个属性,有固定的前缀
	 */
	public static String getProperty(String merchartId, String key) {
		return props.getProperty(merchartId + "_" + key);
	}

	/**
	 * 在控制台上打印出所有属性，调试时用。
	 */
	public static void printProperties() {
		props.list(System.out);
	}
}
